package com.dk.controller;

import com.dk.pojo.TbBrands;
import com.dk.result.Result;
import com.dk.service.TbBrandsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brands")
@RefreshScope      //自动刷新配置, nacos的
@Api
@ApiOperation("后台管理接口")
public class TbBrandsController {
    @Autowired
    private TbBrandsService brandsService;

    //查询品牌
    @GetMapping("getBrandsList")
    @ApiOperation(value = "查询品牌列表")
    public Result getBrandsList(){
        try {
            List<TbBrands> brandsList = brandsService.getBrandsList();
            return Result.OK().setData(brandsList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    //根据id查询: http://127.0.0.1:8090/brands/getBrandsById?id=1
    @GetMapping("getBrandsById")
    @ApiOperation(value = "根据id查询品牌")
    @ApiImplicitParam(name="id",value = "主键id",required = true, paramType = "query")
    public Result getBrandsById(@RequestParam Long id){
        try {
            TbBrands brands = brandsService.getBrandsById(id);
            return Result.OK().setData(brands);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    //增加品牌
    @PostMapping("addBrands")
    @ApiOperation(value = "增加品牌")
    @ApiImplicitParam(name="brands",value = "品牌实体数据",required = true, paramType = "body")
    public Result addBrands(@RequestBody TbBrands brands){
        try {
            brandsService.addBrands(brands);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    //修改品牌
    @PutMapping ("updateBrandsById")
    @ApiOperation(value = "修改品牌数据")
    @ApiImplicitParam(name="brands",value = "品牌实体数据",required = true, paramType = "body")
    public Result updateBrandsById(@RequestBody TbBrands brands){
        try {
            brandsService.updateBrandsById(brands);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    //删除品牌   http://127.0.0.1:8090/brands/deleteBrands/2
    @DeleteMapping("deleteBrands/{id}")
    @ApiOperation(value = "根据商品id删除品牌")
    @ApiImplicitParam(name="id",value = "品牌主键",required = true, paramType = "path")
    public Result deleteBrands(@PathVariable Long id){
        try {
            brandsService.deleteBrands(id);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
}
